﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Polly.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.IQiData;
using WMS.Seven.IQiData.Dto;
using WMS.Seven.Models;
using WMS.Seven.ProductSaleDetils.Dto;

namespace WMS.Seven.Qi
{
    [ApiExplorerSettings(GroupName = "王佳琪")]
    public class QiDataService: ApplicationService,IQiDataService
    {
        private readonly IRepository<Sales, int> _saleRepository;
        private readonly IRepository<SaleDetils, int> _saledatilsRepository;
        private readonly IRepository<Products, int> _productRepository;
        private readonly IRepository<Procurement, int> _procurementRepository;

        public QiDataService(IRepository<Sales, int> saleRepository, IRepository<SaleDetils, int> saledatilsRepository, IRepository<Products, int> productRepository, IRepository<Procurement, int> procurementRepository)
        {
            _saleRepository = saleRepository;
            _saledatilsRepository = saledatilsRepository;
            _productRepository = productRepository;
            _procurementRepository = procurementRepository;
        }



        public async Task<string> GetSale(string SalesOrderCode = "", int page = 1, int limit = 3)
        {
            var salelist = await _saleRepository.GetListAsync();
            var result = salelist.Where(c => c.SalesOrderCode.Contains(SalesOrderCode))
                .Skip((page - 1) * limit)
            .Take(limit)
                .ToList();
            return JsonConvert.SerializeObject(new PageResultDTO<Sales> { code = "0", data = result, count = result.Count, msg = "成功" });
        }
        /// <summary>
        /// 采购退货单查询
        /// </summary>
        /// <param name="SalesOrderCode"></param>
        /// <param name="ProcurementCode"></param>
        /// <param name="ProductName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<string> GetSales(string SalesOrderCode="", string ProcurementCode="", string ProductName="", int page = 1, int limit = 10)
        {
            var salesList = await _saleRepository.GetListAsync();
            var saleDetailsList = await _saledatilsRepository.GetListAsync();
            var productList = await _productRepository.GetListAsync();
            var procurement = await _procurementRepository.GetListAsync();

            var list = from a in salesList
                       join b in saleDetailsList on a.SalesOrderCode equals b.SalesOrderCode
                       join c in productList on b.ProductCode equals c.ProductCode
                       join d in procurement on b.ProcurementCode equals d.ProcurementCode
                       select new SaleDTO
                       {

                         Id=a.Id,
                         SalesOrderCode = a.SalesOrderCode,
                         OutQuantity = a.OutQuantity,
                         ProductCode=b.ProductCode,
                         ReturnQuantity = a.ReturnQuantity,
                         ReturnOutQuantity = a.ReturnOutQuantity,
                         CreateTime = a.CreateTime,
                         Maker = a.Maker,
                         MakeTime = a.MakeTime,
                         Remark = a.Remark,
                         DeliveryDate=a.DeliveryDate,
                         CustomerCode = a.CustomerCode,
                         CustomerName = a.CustomerName,
                         CustomerContact = a.CustomerContact,
                         CustomerPhone = a.CustomerPhone,
                         ProductName = c.ProductName,
                         ProductSpecification = c.ProductSpecification,
                         Category = c.Category,
                         Unit = c.Unit,
                         Quantity= b.Quantity,
                         UnitPrice = c.UnitPrice,
                         //SaleSum = (b.Quantity * c.UnitPrice)- (a.ReturnQuantity * c.UnitPrice),
                         ProcurementCode = d.ProcurementCode,
                         SupplierName = d.SupplierName,
                         DocumentType=d.DocumentType,
                         SupplierCode=d.SupplierCode,
                         CreateDate=d.CreateDate,
                         SaleCode=d.SaleCode,
                         ReceivingDate=d.ReceivingDate,
                         ClientName=d.ClientName,
                         Contact=d.Contact,
                         Phone=d.Phone,
                         Buyer=d.Buyer,
                         ProcurementDate=d.ProcurementDate,

                       };
            
            
            var result = list.Where(c => c.SalesOrderCode.Contains(SalesOrderCode))
                .Where(c => c.ProcurementCode.Contains(ProcurementCode))
                .Where(c => c.ProductName.Contains(ProductName))
               .Skip((page - 1) * limit)
           .Take(limit)
               .ToList();
            return JsonConvert.SerializeObject(new PageResultDTO<SaleDTO> { code = "0", data = result, count = result.Count, msg = "成功" });
        }







    }
}
